//
//  TradeOrderListModel.swift
//  Exchange
//
//  Created by rui on 2018/6/14.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class TradeOrderListModel: Codable,Modelable {

    ///广告ID
    var advertisingId: Int?
    ///金额
    var amount: Double?
    ///申诉时间
    var appealTime: String?
    ///买家id
    var buyer: Int?
    var buyerName: String?
    
    ///买家评价
    var buyerEvaluate: Int?
    ///创建时间
    var createdAt: String?
    ///法币
    var currency: String?
    ///订单id
    var id: Int?
    ///是否申诉 0否1是
    var isAppeal: Int8?
    ///是否评价 0否 1买家已评价 2卖家已评价 3双方互评
    var isEvaluate: Int8?
    ///是否超时 0否1是
    var isOvertime: Int8?
    ///币种
    var moneyType: String?
    ///订单号
    var number: String?
    ///支付确认时间
    var payConfirmTime: String?
    ///支付时间
    var payDeadline: String?
    ///支付状态（0 未付款 1 已付款 2 放币）
    var payStatus: Int8?
    ///用户头像
    var portrait: String?
    ///数量
    var quantity: Double?
    
    ///放币时间
    var releaseDeadline: String?
    ///卖价id
    var seller: Int32?
    ///卖家名称
    var sellName: String?
    ///卖家评价
    var sellerEvaluate: Int8?
    ///状态（0 进行中 1 已完成）
    var status: Int8?
    ///类型（0 购买 1 出售）
    var type: Int8?
    ///单价
    var unitPrice: Double?
    ///修改时间
    var updatedAt: String?
    ///用户名
    var userName: String?
    ///用户id
    var userId: Int?
    ///支付方式 2 银联 1 支付宝
    var payMethod: Int?
    ///留言
    var message: String?
    ///是否存在未读消息
    var isNewMessage: Bool?
    
    ///状态文案
    func getState() -> String {
        let status = self.status ?? 0
        let payStatus = self.payStatus ?? 0
        let isEvaluate = self.isEvaluate ?? 0
        let isAppeal = self.isAppeal ?? 0
        var state = ""
        if status == 1 && (payStatus == 0 || payStatus == 1) {
            state = kText("交易取消")
        }else if payStatus == 0 {
            if isAppeal == 1 {
                state = kText("申诉中")
            }else {
                state = kText("待支付")
            }
            
        }else if payStatus == 1 {
            if isAppeal == 1 {
                state = kText("申诉中")
            }else {
                state = kText("待放币")
            }
        }else {
            if isEvaluate == 0 {
               state = kText("待评价")
            }else if isEvaluate == 1 {
                if Int(self.seller ?? 0) == (AppInfo.default.userId ?? 0) {
                    state = kText("待评价")
                }else {
                    state = kText("交易完成")
                }
            }else if isEvaluate == 2 {
                if Int(self.seller ?? 0) == (AppInfo.default.userId ?? 0) {
                    state = kText("交易完成")
                }else {
                    state = kText("待评价")
                }
            }else {
                state = kText("交易完成")
            }
        }
        return state
    }
    
    ///根据状态获取对应的提示信息
    func getStateText() -> String {
        let status = self.status ?? 0
        let payStatus = self.payStatus ?? 0
        let isAppeal = self.isAppeal ?? 0
        var text: String = ""
        if payStatus == 0 {
            if status == 1 {
                text = kText("买家未付款，交易取消")
            }else if isAppeal == 1 {
                text = kText("已在申诉状态,等待平台处理")
            }else {
                text = String(format: kText("%@将在托管中保存30分钟，逾期未付款交易将自动取消"), self.moneyType ?? "")
            }
        }else if payStatus == 1 {
            if  status == 1 {
                text = kText("买家标记已付款，但买家取消了交易")
            }else if self.isAppeal == 1 {
                text = kText("已在申诉状态,等待平台处理")
            }else {
                text = kText("买家标记已付款，等待卖家释放") + (self.moneyType ?? "")
            }
        }else {
            if self.isEvaluate == 0 {
                text = kText("请对交易评价")
            }else if self.isEvaluate == 1 {
                if Int(self.seller ?? 0) == (AppInfo.default.userId ?? 0) {
                    text = kText("请对交易评价")
                }else {
                    text = kText("交易完成，已完成评价")
                }
            }else if self.isEvaluate == 2 {
                if Int(self.seller ?? 0) == (AppInfo.default.userId ?? 0) {
                    text = kText("交易完成，已完成评价")
                }else {
                    text = kText("请对交易评价")
                }
            }else {
                text = kText("交易完成，已完成评价")
            }
        }
        return text
    }
}
